"""
提交链接：https://leetcode.cn/problems/house-robber-ii/
lc213.打家劫舍II
mudium,吴奇峰 2024.9.19
动态规划
"""


class Solution:
    def rob(self, nums: [int]) -> int:
        if len(nums) == 1: return nums[0]

        def my_rob(nums):
            cur, pre = 0, 0
            for num in nums:
                tmp = cur
                cur = max(num + pre, cur)
                pre = tmp
            return cur

        return max(my_rob(nums[:-1]), my_rob(nums[1:]))